import { tsl_MRParser, ExpressionContext, TslContext } from "../ANTLR/tsl_MRParser";
import { tsl_MRLexer } from "../ANTLR/tsl_MRLexer";
import { ANTLRInputStream, CommonTokenStream } from "antlr4ts";
import TodoLangErrorListener, { ITodoLangError } from "./CCMLErrorListener";

function parse(code: string): {ast:TslContext, errors: ITodoLangError[]} {
    const inputStream = new ANTLRInputStream(code);
    const lexer = new tsl_MRLexer(inputStream);
    lexer.removeErrorListeners()
    const todoLangErrorsListner = new TodoLangErrorListener();
    lexer.addErrorListener(todoLangErrorsListner);
    const tokenStream = new CommonTokenStream(lexer);
    const parser = new tsl_MRParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(todoLangErrorsListner);
    const ast =  parser.tsl();
    const errors: ITodoLangError[]  = todoLangErrorsListner.getErrors();
    console.log(errors);
    return {ast, errors};
}
export function parseAndGetASTRoot(code: string): TslContext {
    const {ast} = parse(code);
    return ast;
}
export function parseAndGetSyntaxErrors(code: string): ITodoLangError[] {
    const {errors} = parse(code);
    return errors;
}